import { ElNotification } from "element-plus";
import { SimulateController } from "./Controller";
import { SimType } from "./types/Controller";

export default class InstanceTable {

  controller: SimulateController;

  constructor(c: SimulateController) {
    this.controller = c;
  }

  async sim() {
    try {
      let modelIds = Array.isArray(this.controller.modelId)
        ? [...this.controller.modelId]
        : [this.controller.modelId];
      if ( this.controller.type === SimType.TCommon) {
        await this.controller.simApi.instancetableEvaluateInstanceTable({
          simulationSessionId: this.controller.sessionId,
          instanceIds: modelIds
        });
      } else if (this.controller.type === SimType.TContext) {
        const res = await this.controller.simApi.instancetableEvaluateInstanceTableWithBehavior({
          simulationSessionId: this.controller.sessionId,
          instanceIds: modelIds
        });
        if (res.data.code !== 1000) {
          throw new Error(res.data.message);
        }
      } else if (this.controller.type === SimType.TSCommon) {
        await this.controller.simApi.instancetableEvaluateInfoFlowTable({
          simulationSessionId: this.controller.sessionId,
          instanceIds: modelIds
        });
      }
    } catch (err: any) {
      if (err) {
        ElNotification({
          title: this.controller.locale.localeTrans("ex_fail"),
          message: err.message || this.controller.locale.localeTrans("opr_error"),
          type: "warning",
        });
      }
    }
  }

  isTableSim(type: SimType) {
    return [SimType.TCommon, SimType.TContext, SimType.TSCommon].includes(type);
  }

  isTableInstanceSim(type: SimType) {
    return [SimType.TCommon, SimType.TContext].includes(type);
  }
}
